package org.onvivals.module.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.onvivals.common.param.PageParam;
import org.onvivals.common.web.PageResult;
import org.onvivals.common.web.Result;
import org.onvivals.module.system.entity.SysRole;
import org.onvivals.module.system.param.SysRoleParam;
import org.onvivals.module.system.service.ISysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "角色管理")
@RestController
@RequestMapping("/system/role")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysRoleController {

    private final ISysRoleService roleService;

    @Operation(summary = "分页查询全部角色")
    @GetMapping("/page")
    public Result<PageResult<SysRole>> page(SysRoleParam param) {
        PageParam<SysRole, SysRoleParam> page = new PageParam<>(param);
        IPage<SysRole> pa = roleService.page(page, page.getWrapper());
        PageResult<SysRole> pageResult = new PageResult<>(pa.getRecords(), pa.getTotal());
        return Result.OK("查询成功", pageResult);
    }


    @Operation(summary = "查询全部角色")
    @GetMapping()
    public Result<List<SysRole>> list(SysRoleParam param) {
        PageParam<SysRole, SysRoleParam> page = new PageParam<>(param);
        List<SysRole> roleList = roleService.list(page.getOrderWrapper());
        return Result.OK("查询成功", roleList);
    }

    @Operation(summary = "根据id查询角色")
    @GetMapping("/{id}")
    public Result<SysRole> get(@PathVariable("id") Long id) {
        SysRole role = roleService.getById(id);
        return Result.OK("查询成功", role);
    }

    @Operation(summary = "添加角色")
    @PostMapping()
    public Result<?> save(@RequestBody SysRole role) {
        if (roleService.count(new LambdaQueryWrapper<SysRole>().eq(SysRole::getRoleCode, role.getRoleCode())) > 0) {
            return Result.error("角色标识已存在");
        }
        if (roleService.count(new LambdaQueryWrapper<SysRole>().eq(SysRole::getName, role.getName())) > 0) {
            return Result.error("角色名称已存在");
        }
        if (roleService.save(role)) {
            return Result.OK("添加成功");
        }
        return Result.error("添加失败");
    }

    @Operation(summary = "修改角色")
    @PutMapping()
    public Result<?> update(@RequestBody SysRole role) {
        if (role.getRoleCode() != null && roleService.count(new LambdaQueryWrapper<SysRole>()
                .eq(SysRole::getRoleCode, role.getRoleCode())
                .ne(SysRole::getRoleId, role.getRoleId())) > 0) {
            return Result.error("角色标识已存在");
        }
        if (role.getName() != null && roleService.count(new LambdaQueryWrapper<SysRole>()
                .eq(SysRole::getName, role.getName())
                .ne(SysRole::getRoleId, role.getRoleId())) > 0) {
            return Result.error("角色名称已存在");
        }
        if (roleService.updateById(role)) {
            return Result.OK("修改成功");
        }
        return Result.error("修改失败");
    }


    @Operation(summary = "删除角色")
    @DeleteMapping("/{id}")
    public Result<?> remove(@PathVariable("id") Long id) {
        if (roleService.removeById(id)) {
            return Result.OK("删除成功");
        }
        return Result.error("删除失败");
    }

}
